import { _decorator, Component, Node, v2, Vec2, Vec3, tween, SystemEvent, Animation, v3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('brid')
export class brid extends Component {
	hp:number = 1
	target:Vec2 = null
	speed:number = 50
	diecallback:Function
	addScoreCallBack:Function
    start() {
		this.fly()
    }
	fly(){
		this.target = v2(Math.random()*500-385,210)
		if(this.target.x<0){
			this.node.setScale(new Vec3(-1,0,0))
		}
		let move = tween(this.node.getPosition())
		.to(3, this.target, {
				onUpdate: (target: Vec3, ratio: number) => {
					this.node.setPosition(target)
				}
			})
		.call(()=>{
			this.diecallback()
		})
		move.start()

		this.node.on(SystemEvent.EventType.TOUCH_START,(e)=>{
			if(this.hp>0){
				this.addScoreCallBack()
				this.hp -=1
				move.stop()
				let ani = this.node.getComponent(Animation)
				ani.play('die')

				let down_die = tween(this.node.getPosition())
				.to(1,v3(this.node.getPosition().x,15,0),{
					onUpdate:(target:Vec3,ratio:number)=>{
						this.node.setPosition(target)
						if(ratio==1){
							this.node.destroy()
						}
					}
				})
				down_die.start()
				// if(this.node.getPosition().y <=15){
				// 	this.node.destroy()

				// }
				
			}
		})
	}
    update(deltaTime: number) {
				
    }
}

